package models

import (
	"ginadmin/db"
	"github.com/jinzhu/gorm"
)

//角色表
type Role struct {
	gorm.Model
	Name   string //名称
	Auth   string //授权--主要用于菜单---不同菜单之间用逗号分隔
	Status int32  //角色状态
}

func (m *Role) Add() error {
	err := new(error)
	DB := db.GetInstance().GetDB()
	if *err = DB.Create(m).Error; err != nil {
		return *err
	}
	return *err
}

func (m *Role) Dels(ids string) error {
	err := new(error)
	DB := db.GetInstance().GetDB()
	if *err = DB.Unscoped().Delete(m, "id in ("+ids+")").Error; err != nil {
		return *err
	}
	return *err
}

func FindRoleByName(name string) bool {
	DB := db.GetInstance().GetDB()
	DB = DB.Where("name=?", name)
	result, _ := db.IsExit(DB, &Role{})
	return result
}

func (role *Role) FindById(id uint) error {
	DB := db.GetInstance().GetDB()
	err := DB.First(role, id).Error
	return err
}

func RoleList() (role []Role, err error) {
	DB := db.GetInstance().GetDB()
	err = DB.Find(&role).Error
	if err != nil {
		return role, err
	}
	return role, err
}
